import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams} from 'ionic-angular';
import {HttpServicesProvider} from "../../providers/http-services/http-services";
import { ConfigProvider } from '../../providers/config/config';
import { UtilsProvider } from '../../providers/utils/utils';
import {ValidateProvider} from "../../providers/validate/validate";
import {WechatProvider} from "../../providers/wechat/wechat";
import {StorageProvider} from "../../providers/storage/storage";

@IonicPage({
    segment: 'service/operation/detail/:id',
    defaultHistory: ['HomePage','OperationPage']
})
@Component({
    selector: 'page-operation-detail',
    templateUrl: 'operation-detail.html',
})
export class OperationDetailPage {
    //点击收藏的数据
    sendData = {
        token: '',
        amoy_id: 0,
        type: 1
    };
    params = {
        amoyId: 0
    };
    infoData = {};
    collectionStatus = '收藏';

    constructor(
        public navCtrl: NavController,
        public navParams: NavParams,
        public httpService: HttpServicesProvider,
        public config: ConfigProvider,
        public util: UtilsProvider,
        public storage: StorageProvider,
        public validate: ValidateProvider,
        public wechat: WechatProvider
    ) {
    }
    ionViewCanEnter(){
        return this.validate.checkLogin();
    }
    ionViewWillEnter(){
        //文章ID
        this.params.amoyId = this.navParams.get('id');
        this.getData();
    }

    getData() {
        let apiUrl = "/service/get_info_detail";
        this.httpService.requestPostData(apiUrl, {
            amoy_id: this.params.amoyId,
            token: this.config.getToken()
        }, response => {
            this.infoData = response['data'];

            if(this.infoData['has_collect'] == 1) {
                this.collectionStatus = '已收藏';
            }

            this.wechat.getWxConfig(()=>{
                // this.wechat.wxShareMoments({shareNumber:3, shareTitle:this.infoData['title'], amoyId: this.params.amoyId});
                // this.wechat.wxShareFriend({shareNumber:3, shareTitle:this.infoData['title'], amoyId: this.params.amoyId});
                this.shareResult();
            });
        });
    }

    doCollection() {
        let apiUrl = "/task_info/collect_option";
        this.sendData.amoy_id = this.params.amoyId;
        this.sendData.type = 1;
        this.sendData.token = this.config.getToken();
        this.httpService.requestPostData(apiUrl, this.sendData, data => {
            if(data['code'] == 200 && this.collectionStatus == '收藏') {    
                this.collectionStatus = '已收藏';
                this.util.presentAlert('操作成功');
            } else if(data['code'] == 200 && this.collectionStatus == '已收藏') {
                this.collectionStatus = '收藏';
                this.util.presentAlert('操作成功');
            } else {
                this.util.presentAlert(data.message);
            }
        })
    }

    /**
     * 分享结果
     * */
    shareResult(title=''){
        if(title == '' || title == undefined){
            title = this.infoData['title'];
        }
        let userInfo = JSON.parse(this.storage.get('userInfo'));
        //分享到朋友圈
        this.wechat.wxShareMoments({shareNumber: 3, amoyId: this.params.amoyId, shareTitle: title, userId:userInfo.user_id}, ()=>{
            let apiUrl = "/task_info/insert_share";
            this.httpService.requestPostData(apiUrl, {
                token:this.config.getToken(),
                amoy_id: this.params.amoyId
            },() => {
                this.getData();
            });
        });

        //分享到朋友
        this.wechat.wxShareFriend({shareNumber: 3, amoyId: this.params.amoyId, shareTitle: title, userId:userInfo.user_id}, ()=>{
            let apiUrl = "/task_info/insert_share";
            this.httpService.requestPostData(apiUrl, {
                token:this.config.getToken(),
                amoy_id: this.params.amoyId
            },() => {
                this.getData();
            });
        });
    }
}
